﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TravelAgencyDAL;

namespace Beginning
{
    public partial class frmDeleteWayStation : Form
    {
        public long wayStationID
        {
            get;
            set;
        }

        public long destID
        {
            get;
            set;
        }

        public frmDeleteWayStation(long wayStationID, long destID)
        {
            InitializeComponent();
            this.wayStationID = wayStationID;
            this.destID = destID;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool confirm =
                MessageBox.Show("Are You Sure?", "Confirmation", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes;
            if (confirm)
            {
                DAO.DeleteWayStation(wayStationID, Convert.ToInt32(txtNewDistanceTo.Text),
                    (controlNewTimeTo.Hours * 60) + (controlNewTimeTo.Minutes),
                    Convert.ToInt32(txtNewStay.Text), Convert.ToDecimal(txtNewPriceTo.Text));

                frmDestinationDetails details = new frmDestinationDetails(destID);
                details.Show();
                this.Hide();
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            frmDestinationDetails details = new frmDestinationDetails(destID);
            details.Show();
            this.Hide();
        }

        private void frmDeleteWayStation_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmDestinationDetails details = new frmDestinationDetails(destID);
            details.Show();
            this.Hide();
        }

        #region Validating

        private void txtNewDistanceTo_Validating(object sender, CancelEventArgs e)
        {
            ValidateNewDistTo();
        }

        private void txtNewStay_Validating(object sender, CancelEventArgs e)
        {
            ValidateNewPriceTo();
        }

        private void txtNewPriceTo_Validating(object sender, CancelEventArgs e)
        {
            ValidateNewStay();
        }

        private bool ValidateNewDistTo()
        {
            if (IsNewDistToValid(txtNewDistanceTo.Text))
            {
                epValidateData.SetError(txtNewDistanceTo, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtNewDistanceTo, "Not a valid distance!");
                return false;
            }
        }

        private bool IsNewDistToValid(string distance)
        {
            try
            {
                if (Int32.Parse(distance) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (FormatException)
            {
                return false;
            }
        }

        private bool ValidateNewPriceTo()
        {
            if (IsNewPriceToValid(txtNewPriceTo.Text))
            {
                epValidateData.SetError(txtNewPriceTo, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtNewPriceTo, "Not a valid price!");
                return false;
            }
        }

        private bool ValidateNewStay()
        {
            if (IsNewPriceToValid(txtNewStay.Text))
            {
                epValidateData.SetError(txtNewStay, "");
                return true;
            }
            else
            {
                epValidateData.SetError(txtNewStay, "Not valid minutes for stay!");
                return false;
            }
        }

        private bool IsNewPriceToValid(string price)
        {
            try
            {
                if (Decimal.Parse(price) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (FormatException)
            {
                return false;
            }
        }

        #endregion
    }
}
